package cn.micro.system.manage.annotate.impl;

import cn.micro.system.manage.annotate.UniquePosition;
import cn.micro.system.manage.entity.SysDimPosition;
import cn.micro.system.manage.service.SysDimPositionService;
import javax.annotation.Resource;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;
import java.util.function.Predicate;

public class PositionValidation  <T extends Annotation> implements ConstraintValidator<T, SysDimPosition> {

    protected Predicate<SysDimPosition> predicate = c -> true;

    @Resource
    protected SysDimPositionService sysDimPositionService;

    @Override
    public boolean isValid(SysDimPosition sysDimPosition, ConstraintValidatorContext constraintValidatorContext) {
        return sysDimPositionService == null || predicate.test(sysDimPosition);
    }

    /**
     * 校验角色编码或者角色名称是否重复
     */
    public static class UniquePositionValidate extends PositionValidation<UniquePosition>{
        @Override
        public void initialize(UniquePosition constraintAnnotation) {
            predicate = c -> sysDimPositionService.getByCodeOrName(c).isEmpty();
        }
    }

}
